import 'package:flutter/material.dart';
import 'package:neffos/neffos.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Home")),
      body: Column(
        children: [MaterialButton(onPressed: () => conn(), child: Text("conn"))],
      ),
    );
  }

  conn() async {
    Conn conn = await dial(
      "ws://192.168.200.201:8082/default/live_course",
      connHandler: {
        "live": {
          OnNamespaceConnect: (NsConn ns, Message msg) async {
            print("OnNamespaceConnect:连接成功");
            // print(msg.body);
            return null;
          },
          OnNamespaceDisconnect: (NsConn ns, Message msg) {
            print("OnNamespaceDisconnect:");
            return null;
          }
        }
      },
      connHeader: {"Authorization": "xvb6pzASxH9i9wtciiX/6NUpuWuuEE3JacpLnKlIPelVIz6HRLlwDW9l6sM2MTITjY7ebi73e0MCkatXP5nzGA=="},
    );

    try {
      NsConn nsConn = await conn.connect("live");
      print("namespace:123");
      print(nsConn.namespace);
    } catch (e) {
      print(e);
    }

    // Room room = await nsConn.joinRoom("2");
    // print("name:456");
    // print(room.name);
  }
}
